package com.example.administrator.demoa;

import android.app.IntentService;
import android.content.Intent;
import android.content.Context;
import android.util.Log;

public class MyIntentService extends IntentService {
    public MyIntentService() {
        super("MyIntentService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        //新开一个线程，适合耗时操作
        Log.e("onHandleIntent", "thread id = "+ Thread.currentThread().getId());

        int i = 0;
        while(i<=30){
            Log.e("MyService", "i = "+ i);
            i++;

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        //onHandleIntent执行完自动销毁
        Log.e("MyIntentService", "onDestroy");
    }

    @Override
    public void onCreate() {
        super.onCreate();
        //创建时依旧为主线程
        Log.e("MyIntentServiceOnCreate", "thread id = "+ Thread.currentThread().getId());
    }
}
